package com.fm.webrtc.websocket.message;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.lang.reflect.Type;

public class MessageUtil {
    public static final String TAG = MessageUtil.class.getSimpleName();
    public static  <T> String toJson(T t) {
        Gson gson = new Gson();
        return gson.toJson(t);
    }

    public static <T> String encoder(String type, T data) {
        Message<T> message = new Message<T>(type, data);
        Log.e(TAG, toJson(message));
       return toJson(message);
    }

    public static <T> Message<T> decoder(String message,Type type){
        Gson gson = new Gson();
        Log.e(TAG, message);

        return gson.fromJson(message, type);
    }

    public static <T> T jsonToObject(JsonObject data, Class<T> type){
        Gson gson = new Gson();
        return gson.fromJson(data, type);
    }
}
